package com.wwwarehouse.common.common;

import android.app.Activity;
import android.content.Context;

import com.wwwarehouse.common.bean.UpVersionBean;
import com.wwwarehouse.common.eventbus_event.ShareResultEvent;

import java.util.ArrayList;
import java.util.HashMap;

import cn.sharesdk.framework.Platform;
import cn.sharesdk.framework.Platform.ShareParams;
import cn.sharesdk.framework.PlatformActionListener;
import cn.sharesdk.framework.ShareSDK;
import cn.sharesdk.tencent.qq.QQ;
import cn.sharesdk.tencent.qzone.QZone;
import cn.sharesdk.wechat.friends.Wechat;
import cn.sharesdk.wechat.moments.WechatMoments;
import de.greenrobot.event.EventBus;

public class Common {
    private static Common instance;
    private boolean bluetoothState;

    // 获取单例
    public static Common getInstance() {
        synchronized (Common.class) {
            if (instance == null) {
                instance = new Common();
                activityList = new ArrayList<>();
            }
        }

        return instance;
    }

    // Activity栈
    private static ArrayList<Activity> activityList;

    // 把Activity添加进栈
    public void addActivity(Activity activity) {
        activityList.add(activity);
    }

    // 把Activity从栈中移除
    public void removeActivity(Activity activity) {
        try {
            activityList.remove(activity);
        } catch (Exception e) {

        }
    }

    // 结束栈中的所有Activity
    public void finishAllActivity() {
        if (activityList != null && activityList.size() != 0) {
            for (int i = activityList.size() - 1; i >= 0; i--) {
                try {
                    activityList.get(i).finish();
                } catch (Exception e) {

                }
            }
            activityList.clear();
        }
    }

    long lastClickTime = 0;

    // 判断是不是快速点击，返回true表示不是
    public boolean isNotFastClick() {
        long time = System.currentTimeMillis();
        if (time - lastClickTime >= 400) {
            lastClickTime = time;
            return true;
        } else {
            return false;
        }
    }

    // 分享的回调
    PlatformActionListener platformActionListener = new PlatformActionListener() {
        @Override
        public void onComplete(Platform platform, int i, HashMap<String, Object> hashMap) {
            if (platform.getName().equals(WechatMoments.NAME)) {
                EventBus.getDefault().post(new ShareResultEvent("weixin_moment_success"));
            } else if (platform.getName().equals(Wechat.NAME)) {
                EventBus.getDefault().post(new ShareResultEvent("weixin_friend_success"));
            }
        }

        @Override
        public void onError(Platform platform, int i, Throwable throwable) {
            if (platform.getName().equals(WechatMoments.NAME)) {
                EventBus.getDefault().post(new ShareResultEvent("weixin_moment_failed"));
            } else if (platform.getName().equals(Wechat.NAME)) {
                EventBus.getDefault().post(new ShareResultEvent("weixin_friend_failed"));
            }
        }

        @Override
        public void onCancel(Platform platform, int i) {
            if (platform.getName().equals(WechatMoments.NAME)) {
                EventBus.getDefault().post(new ShareResultEvent("weixin_moment_cancel"));
            } else if (platform.getName().equals(Wechat.NAME)) {
                EventBus.getDefault().post(new ShareResultEvent("weixin_friend_cancel"));
            }
        }
    };

    // 分享到微信朋友
    public void shareWeixinFriend(Context context, String title, String text, String imageUrl, String clickUrl) {
        ShareParams wechat = new ShareParams();
        wechat.setTitle(title);
        wechat.setText(text);
        wechat.setImageUrl(imageUrl);
        wechat.setUrl(clickUrl);
        wechat.setShareType(Platform.SHARE_WEBPAGE);
        Platform weixin = ShareSDK.getPlatform(context, Wechat.NAME);
        weixin.setPlatformActionListener(platformActionListener);
        weixin.share(wechat);
    }

    // 分享到微信朋友圈
    public void shareWeixinMoment(Context context, String title, String text, String imageUrl, String clickUrl) {
        ShareParams wechat = new ShareParams();
        wechat.setTitle(title);
        wechat.setText(text);
        wechat.setImageUrl(imageUrl);
        wechat.setUrl(clickUrl);
        wechat.setShareType(Platform.SHARE_WEBPAGE);
        Platform weixin = ShareSDK.getPlatform(context, WechatMoments.NAME);
        weixin.setPlatformActionListener(platformActionListener);
        weixin.share(wechat);
    }

    // 分享到微信朋友
    public void shareWeixinIMGFriend(Context context, String imageUrl) {
        ShareParams wechat = new ShareParams();
        wechat.setImageUrl(imageUrl);
        String[] imgArr = {imageUrl, imageUrl};
        wechat.setImageArray(imgArr);
        wechat.setShareType(Platform.SHARE_IMAGE);
        Platform weixin = ShareSDK.getPlatform(context, WechatMoments.NAME);
        weixin.setPlatformActionListener(platformActionListener);
        weixin.share(wechat);
    }

    // 分享到微信朋友圈
    public void shareWeixinTextMoment(Context context, String text) {
        ShareParams wechat = new ShareParams();
        wechat.setText(text);
        wechat.setShareType(Platform.SHARE_TEXT);
        Platform weixin = ShareSDK.getPlatform(context, Wechat.NAME);
        weixin.setPlatformActionListener(platformActionListener);
        weixin.share(wechat);
    }

    // 分享到QQ
    public void shareQQ(Context context, String title, String text, String imageUrl, String clickUrl) {
        ShareParams params = new ShareParams();
        params.setTitle(title);
        params.setText(text);
        params.setImageUrl(imageUrl);
        params.setUrl(clickUrl);
        params.setTitleUrl(clickUrl);
        params.setShareType(Platform.SHARE_WEBPAGE);
        Platform qq = ShareSDK.getPlatform(context, QQ.NAME);
        qq.setPlatformActionListener(platformActionListener);
        qq.share(params);
    }

    // 分享到QQ空间
    public void shareQQZone(Context context, String title, String text, String imageUrl, String clickUrl) {
        ShareParams params = new ShareParams();
        params.setTitle(title);
        params.setText(text);
        params.setImageUrl(imageUrl);
        params.setUrl(clickUrl);
        params.setTitleUrl(clickUrl);
        params.setSite("网仓");
        params.setSiteUrl("http://www.wwwarehouse.com/");
        params.setShareType(Platform.SHARE_WEBPAGE);
        Platform qzone = ShareSDK.getPlatform(context, QZone.NAME);
        qzone.setPlatformActionListener(platformActionListener);
        qzone.share(params);
    }

    boolean isReceivedTask = false;

    // 判断是否是接收的任务
    public void setReceiveTask(boolean isReceived) {
        isReceivedTask = isReceived;
    }

    // 获取是否是接收的任务
    public boolean getReceiveTask() {
        return isReceivedTask;
    }

    /**
     * 获取、保存蓝牙状态
     *
     * @return
     */
    public boolean getBluetoothState() {
        return false;
    }

    public void setBluetoothState(boolean bluetoothState) {
        this.bluetoothState = bluetoothState;
    }

    //是否暂不更新
    boolean isUpdateLater = false;

    public void setUpdateLater(boolean later) {
        isUpdateLater = later;
    }

    public boolean getIsUpdateLater() {
        return isUpdateLater;
    }

    //最新的版本
    private UpVersionBean mUpVersionBean;

    public void setUpVersionBean(UpVersionBean upVersionBean) {
        mUpVersionBean = upVersionBean;
    }

    public UpVersionBean getUpVersionBean() {
        return mUpVersionBean;
    }

}